// packJumpRope/addMineJumpList.js

import {
  getStaticImgUrl
} from '../utils/api.config'
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    modal: null,
    userData: {},
    dataList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: "添加跳绳"
    })

    const userInfo = wx.getStorageSync('userData');
    const stuIndex = wx.getStorageSync('stuIndex');
    const data = userInfo.list[stuIndex]
    this.setData({
      userData: data
    })
    this.getMineJumList()
  },
  returnBusinParams(bindType, imei) { //bindType 绑定类型 1物联网、2蓝牙

    let params = {
      bindType: bindType,
      imei: imei,
      musicMode: 1,
      //musicMode: wx.getStorageSync('musicMode') || 1,
      // type: 1,
      // schoolId: this.data.userData.schoolId,
      // partyId: this.data.userData.partyId,
      // studentId: this.data.userData.studentId
    }
    console.log(JSON.stringify(params));
    return params
  },

  goBindJump() {
    // wx.navigateTo({
    //   url: '/packJumpRope/addMineJump'
    // })
    wx.showToast({
      title: '开发中，敬请期待...',
      icon: 'none'
    })
  },
  checkImei(imei) {
    if (imei && imei.length == 15) {
      var str = imei.substring(0, 2);
      if (str == '86') {

        return true
      } else {
        wx.showToast({
          title: '请扫码正确的跳绳二维码',
          icon: 'none'
        })
        return false
      }

    } else {
      wx.showToast({
        title: '请扫码正确的跳绳二维码',
        icon: 'none'
      })
      return false
    }

  },
  getMineJumList() {
    wx.showLoading({
      title: '加载中...',
      icon: 'none',
    })
    app.api.mySkipRope().then(rest => {
        wx.hideLoading()

        if (rest && rest.success) {
          var list = rest.data
          this.setData({
            dataList: list
          })
        } else {
          wx.showToast({
            title: rest.errMsg || '网络异常，请稍后重试！！',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      })
      .catch(r => {
        console.error(r)
        wx.showToast({
          title: '网络异常，请稍后重试！',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      })

  },

  bindWo() {
    var that = this;
    if (this.data.dataList && this.data.dataList.length >= 1) {

      this.selectComponent("#modal").showModal({
        title: '温馨提示',
        modalType: 0,
        content: '一个学生只可绑定一根物联网跳绳，您已绑定了一根跳绳，请先解绑后再重新绑定',
        topImageUrl: getStaticImgUrl('/new/alrt_collect.png'),
        confirmText:'知道了',
        hideCancelBtn:true,
        complete: (res) => {
          if (res.cancel) {}
          if (res.confirm) {

          }
        }
      })
      return
    }

    wx.scanCode({
      success: (rest) => {
        if (that.checkImei(rest.result)) {
          var str = '学生姓名：' + that.data.userData.name
          var str1 = '跳绳IMEI：' + rest.result

          this.selectComponent("#modal").showModal({
            title: '请确认绑定信息',
            modalType: 0,
            contentList: [str, str1],
            topImageUrl: getStaticImgUrl('/new/alrt_collect.png'),
            complete: (res) => {
              if (res.cancel) {}
              if (res.confirm) {
                that.bindWoData(rest.result)

              }
            }
          })
        }
      },

      fail: (res) => {

        console.log(res);

      }

    })

  },
  bindWoData(imei) {
    wx.showLoading({
      title: '绑定加载中...',
      icon: 'none',
    })

    app.api.addEquipment(this.returnBusinParams(1, imei)).then(rest => {
        wx.hideLoading()

        if (rest && rest.success) {
          wx.showToast({
            title: '绑定成功',
            icon: 'none',
            duration: 2000,
            mask: true
          })
          wx.navigateBack()
        } else {
          wx.showToast({
            title: rest.errMsg || '网络异常，请稍后重试！！',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      })
      .catch(r => {
        console.error(r)
        wx.showToast({
          title: '网络异常，请稍后重试！',
          icon: 'none',
          duration: 2000,
          mask: true
        })
      })

  },
  bindBo() {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.modal = this.selectComponent("#modal")

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }


})